package com.edie.customview.modular;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.edie.customview.R;

public class BlurActivity extends AppCompatActivity implements View.OnClickListener {
    private Button button6;
    private ImageView iv;

    public static void start(Context context) {
        context.startActivity(new Intent(context, BlurActivity.class));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blur);
        initView();

    }

    private void initView() {
        button6 = (Button) findViewById(R.id.button6);
        iv = (ImageView) findViewById(R.id.iv);
        button6.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button6:
                Resources res = getResources();
                Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.instruction_step_2);
//                Bitmap bitmap = blur(getApplicationContext(), bmp, 20f);
                Bitmap bitmap = rsBlur(getApplicationContext(), bmp, 20);
                iv.setImageBitmap(bitmap);
                break;
        }
    }

    public static Bitmap blur(Context context, Bitmap image, float radius) {
        RenderScript renderScript = RenderScript.create(context);
        Bitmap outputBitmap = Bitmap.createBitmap(image.getHeight(), image.getWidth(), Bitmap.Config.ARGB_8888);
        Allocation in = Allocation.createFromBitmap(renderScript, image);
        Allocation out = Allocation.createFromBitmap(renderScript, outputBitmap);

        ScriptIntrinsicBlur intrinsicBlur = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
        intrinsicBlur.setRadius(radius);
        intrinsicBlur.setInput(in);
        intrinsicBlur.forEach(out);

        out.copyTo(outputBitmap);
        image.recycle();
        renderScript.destroy();
        return outputBitmap;
    }

    private static Bitmap rsBlur(Context context, Bitmap source, int radius) {

        Bitmap inputBmp = source;
        RenderScript renderScript = RenderScript.create(context);


        // Allocate memory for Renderscript to work with
        final Allocation input = Allocation.createFromBitmap(renderScript, inputBmp);
        final Allocation output = Allocation.createTyped(renderScript, input.getType());
        // Load up an instance of the specific script that we want to use.
        ScriptIntrinsicBlur scriptIntrinsicBlur = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
        scriptIntrinsicBlur.setInput(input);
        // Set the blur radius
        scriptIntrinsicBlur.setRadius(radius);
        // Start the ScriptIntrinisicBlur
        scriptIntrinsicBlur.forEach(output);
        // Copy the output to the blurred bitmap
        output.copyTo(inputBmp);
        renderScript.destroy();

        return inputBmp;
    }

}
